import 'package:flutter/material.dart';
import 'package:message_tools/application.dart';
import 'package:message_tools/components/appbar_plugins.dart';
import 'package:message_tools/model/sms.dart';
import 'package:message_tools/ui/page/sms/add_sms_task_page.dart';
import 'package:message_tools/ui/page/sms/send_result_page.dart';
import 'package:message_tools/ui/page/sms/sms_task_list_page.dart';
import 'package:message_tools/ui/page/sms/sms_template_list_page.dart';

class SmsHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new _BuildSmsHomePage();
  }
}

class _BuildSmsHomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    var list = Sms.getSmsList();
    List<Widget> lists = [];
    for (var item in list) {
      var border = new Border(
        bottom: new BorderSide(
            width: 0.8, style: BorderStyle.solid, color: Colors.grey),
        right: new BorderSide(
            width: 0.8, style: BorderStyle.solid, color: Colors.grey),
      );

      lists.add(new Container(
          height: 40.0,
          width: 40.0,
          decoration: new BoxDecoration(
            //背景
            color: Colors.white,
            //设置四周边框
            border: border,
          ),
          child: new Center(
              child: GestureDetector(
            onTap: () {
              navigatorTo(context, item.title);
            }, //写入方法名称就可以了，但是是无参的
            child: new Column(
                //平分空白
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: 60,
                    height: 60,
                    child: Image.asset('assets/images/' + item.image),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 5),
                    child: new Text(item.title, style: TextStyle(fontSize: 18)),
                  ),
                ]),
          ))));
    }

    var gridview = GridView.count(
      //一行多少个
      crossAxisCount: 3,
      //滚动方向
      scrollDirection: Axis.vertical,

      //宽高比
      childAspectRatio: 2 / 2,

      //设置itemView
      children: lists,
    );

    return Scaffold(appBar: AppBarPlugin.Create("短信群发王"), body: gridview);
  }
}

void navigatorTo(BuildContext context, String title) {
  var page;

  if (title == "管理任务") {
    page = SmsTaskListPage();
  } else if (title == "短信模板") {
    page = SmsTemplateListPage();
  } else if (title == "添加任务") {
    Application.setString("edit_sms_task", "");
    page = AddSmsTaskPage();
  } else if (title == "发送历史") {
    page = SendResultPage();
  } else {
    return;
  }

  Navigator.push(
    context,
    new MaterialPageRoute(builder: (context) => page),
  );
}
